<template>
  <div class="page-loading" ref="pageLoadingPanel" v-if="!hidden">
    <div class="loading-icon" ref="loadingIcon"></div>
  </div>
</template>
<style lang="scss">
  @import "../sass/var";
  @import "../sass/mixin";

  .page-loading {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    justify-content: center;
    align-items: center;
    background-color: $body-bg;
  }

  .text-tip {
    color: $color-muted;
    font-weight: lighter;
    font-size: 28px;
  }

  .loading-img {
    width: 100px;
    height: 100px;
  }

  .loading-icon {
    width: 60px;
    height: 60px;
    background-color: #dfdfdf;
    border-radius: 5px;
    @include border(1px, solid, #d6d6d6)
  }
</style>
<script>

const animation = weex.requireModule('animation')

module.exports = {
  props: {
    loaded: {default: false}
  },

  data () {
    return {
      hidden: false,
      rotateX: 0,
      rotateY: 0
    }
  },
  components: {},
  mounted () {
    this.step(0)
    this.$watch('loaded', (newVal, oldVal) => {
      newVal && this.hide()
    })
  },
  methods: {
    hide () {
      animation.transition(this.$refs.pageLoadingPanel, {
        styles: {
          opacity: 0
        },
        timingFunction: 'ease',
        duration: 200, // ms
        delay: 0 // ms
      }, () => {
        this.hidden = true
      })
    },
    step (i) {
      this.rotateX = parseInt((i + 1) / 2) * 180
      this.rotateY = parseInt(i / 2) * 180
      // let modal = weex.requireModule('modal')
      // modal.toast({message: JSON.stringify(i + ' ' + this.rotateX + ' ' + this.rotateY)})
      if (this.loaded) {
        this.hide()
        return
      }
      if (this.hidden) {
        return
      }
      animation.transition(this.$refs.loadingIcon, {
        styles: {
          transform: 'perspective(120px) rotateX(' + this.rotateX + 'deg) rotateY(' + this.rotateY + 'deg)'
        },
        duration: 600, // ms
        delay: 0 // ms
      }, () => {
        this.step(i + 1)
      })
    }
  }
}
</script>
